package browserUI
{
	import dev.touchswipe.shared.Fonts;
	
	import flash.display.Bitmap;
	import flash.display.CapsStyle;
	import flash.display.GradientType;
	import flash.display.LineScaleMode;
	import flash.display.Shape;
	import flash.display.Sprite;
	import flash.events.Event;
	import flash.events.MouseEvent;
	import flash.filters.DropShadowFilter;
	import flash.geom.Matrix;
	import flash.text.TextField;
	import flash.text.TextFormat;
	import flash.text.TextFormatAlign;
	
	public class FavouritesPanel extends Sprite
	{
		
		private var _mask:Shape;
		private var _content:Sprite;
		private var _tileContainer:Sprite;
		private var _addFavouriteBtn:Sprite;
		public var open:Boolean;
		
		public function FavouritesPanel()
		{
			super();
			
			_mask=new Shape();
			_mask.graphics.beginFill(0xFFFFFF);
			_mask.graphics.drawRect(-500,0,500,550);
			addChild(_mask);
			
			_content=new Sprite();
			var m:Matrix=new Matrix();
			m.createGradientBox(1,550,Math.PI/2,0,0);
			_content.graphics.beginGradientFill(GradientType.LINEAR,[0xA0A2A5,0xE8E8E8],[1,1],[0,255],m);
			_content.graphics.drawRoundRectComplex(0,0,500,550,0,0,25,0);
			_content.mask=_mask;
			addChild(_content);
			_tileContainer=new Sprite();
			_content.addChild(_tileContainer);
			_content.filters=[new DropShadowFilter(1,90,0,0.7,2,2)];
			
			_addFavouriteBtn=new Sprite();
			_addFavouriteBtn.graphics.beginFill(0xA0A2A5);
			_addFavouriteBtn.graphics.drawCircle(0,0,20);
			_addFavouriteBtn.graphics.beginFill(0xE8E8E8);
			_addFavouriteBtn.graphics.drawRect(-7.5,-2.5,15,5);
			_addFavouriteBtn.graphics.beginFill(0xE8E8E8);
			_addFavouriteBtn.graphics.drawRect(-2.5,-7.5,5,15);
			_addFavouriteBtn.y=550-25;
			_addFavouriteBtn.x=500-25;
			_content.addChild(_addFavouriteBtn);
			_addFavouriteBtn.addEventListener(MouseEvent.CLICK,function(event:MouseEvent):void{
				dispatchEvent(new UICommandEvent(UICommandEvent.CREATE_FAVOURITE));
				drawFavourites();
			});
			
			var titleText:TextField=new TextField();
			titleText.defaultTextFormat=new TextFormat(Fonts.SegoeUIL,30);
			titleText.embedFonts=true;
			titleText.selectable=false;
			titleText.x=15;
			titleText.width=485;
			titleText.height=40;
			titleText.text="Favourites";
			_content.addChild(titleText);
			
			drawFavourites();
			addEventListener(Event.ENTER_FRAME,onEnterFrame);
		}
		
		private function onEnterFrame(event:Event):void{
			var target:Number=open?-500:0;
			if(target!=_content.x){
				_content.x-=(_content.x-target)*0.5;
				if(Math.abs(_content.x-target)<5)
					_content.x=target
			}
		}
		
		public function redraw():void{
			
		}
		
		public function drawFavourites():void{
			_content.removeChild(_tileContainer);
			_tileContainer=null;
			_tileContainer=new Sprite();
			_content.addChild(_tileContainer);
			for(var i:int=0; i<MTBrowser.currentSettings.favourites.length; i++){
				var bmp:Bitmap=new Bitmap();
				if(MTBrowser.currentSettings.favouritesBitmapData[i])
					bmp=new Bitmap(MTBrowser.currentSettings.favouritesBitmapData[i].readBitmapData());
				if(bmp){					
					var mouseSprite:Sprite=new Sprite();
					mouseSprite.name=MTBrowser.currentSettings.favourites[i];
					mouseSprite.x=(400/3+25)*(i%3)+25;
					mouseSprite.y=(100+45)*(Math.floor(i/3))+125;
					mouseSprite.addEventListener(MouseEvent.CLICK,function(event:MouseEvent):void{
						Navbar(parent).url=event.target.name;
						Navbar(parent).browseToURL();
						open=false;
					});
					
					bmp.x=200/3-bmp.width/2;
					bmp.y=-bmp.height/2;
					mouseSprite.addChild(bmp);
					
					
					var text:TextField=new TextField();
					text.defaultTextFormat=new TextFormat(Fonts.SegoeUIL,12,0,null,null,null,null,null,TextFormatAlign.CENTER);
					text.text=MTBrowser.currentSettings.favourites[i];
					text.selectable=false;
					text.embedFonts=true;
					text.width=400/3;
					text.y=50;
					text.height=20;
					mouseSprite.addChild(text);
					
					var deleteBtn:Sprite=new Sprite();
					deleteBtn.graphics.beginFill(0xCC2233);
					deleteBtn.graphics.drawCircle(0,0,17);
					deleteBtn.graphics.endFill();
					deleteBtn.graphics.lineStyle(4,0xFFFFFF,1,false,LineScaleMode.NORMAL,CapsStyle.SQUARE);
					deleteBtn.graphics.moveTo(-6,-6);
					deleteBtn.graphics.lineTo(6,6);
					deleteBtn.graphics.moveTo(-6,6);
					deleteBtn.graphics.lineTo(6,-6);
					deleteBtn.x=mouseSprite.x+bmp.x+bmp.width-10;
					deleteBtn.y=mouseSprite.y+bmp.y+10;
					deleteBtn.name=i.toString();
					deleteBtn.addEventListener(MouseEvent.CLICK,function(event:MouseEvent):void{
						MTBrowser.currentSettings.removeFavourite(parseInt(event.target.name));
						drawFavourites();
					});
					
					_tileContainer.addChild(mouseSprite);
					_tileContainer.addChild(deleteBtn);
					
				}
			}
		}
	}
}